package dbhandler;

import dbhandler.handler.MapHandler;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class Row {
    public static <T> T toBean(ResultSet rst, Class<?> type) {
        return (T) BeanUtil.Bean(rst, type);
    }

    public static Map<String,Object> toMap(ResultSet rs) throws SQLException {
        //创建一个集合用于存放结果集的列名和列值
        Map<String,Object> map=new HashMap<String, Object>();
        //获取结果集的所有列名和列值
        ResultSetMetaData metaData = rs.getMetaData();
        //循环列的长度
        for (int i=1;i<metaData.getColumnCount();i++){
            //给map集合赋值
            map.put(metaData.getColumnLabel(i),rs.getObject(i));
        }
        return map;
    }

    public static Object[] toArray(ResultSet rs) throws SQLException {
        //创建一个数组对象，数组对象的长度等于结果集的长度
        Object[] objects=new Object[rs.getMetaData().getColumnCount()];
        for (int i=0;i<objects.length;i++){
            //给Object数组赋值
            objects[i]=rs.getObject(i+1);
        }
        return objects;

    }
}
